knitr::opts_chunk$set(echo = TRUE)

Task 1

Get working directory

getwd()

Task 2

spruce.df = read.csv("SPRUCE.csv")
head(spruce.df)

Task 3

Scatterplot Height~BHDiameter

with(spruce.df,plot(Height~BHDiameter,bg="Blue",pch=21,cex=1.2,xlim=c(0, 1.1*max(BHDiameter)),ylim=c(0, 1.1*max(Height))))

Lowess smoother scatter plot in layout

library(s20x)
layout(matrix(1:3,nr=3,nc=1,byrow=T,))
trendscatter(Height~BHDiameter,f=0.5, data=spruce.df, main = "Plot of Height vs Diameter (lowess+/-sd) f=0.5")
trendscatter(Height~BHDiameter,f=0.6, data=spruce.df, main = "Plot of Height vs Diameter (lowess+/-sd) f=0.6")
trendscatter(Height~BHDiameter,f=0.7, data=spruce.df, main = "Plot of Height vs Diameter (lowess+/-sd) f=0.7")

Use lm() to create spruce.lm and scatter plot with abline()

spruce.lm <- lm(Height~BHDiameter, data = spruce.df)
plot(Height~BHDiameter, data = spruce.df)
abline(spruce.lm)

Is a straight line appropriate? I think the curve is smooth enough that we can use straight line.

Task 4

Four charts

layout(matrix(1:4,nr=2,nc=2,byrow=TRUE))

### First graph

with(spruce.df,
     plot(Height~BHDiameter,bg="Blue",pch=21,ylim=c(0,1.1*max(Height)),xlim=c(0,1.1*max(BHDiameter)))
)
abline(spruce.lm)

### Second graph

with(spruce.df,
     plot(Height~BHDiameter,bg="Blue",pch=21,ylim=c(0,1.1*max(Height)),xlim=c(0,1.1*max(BHDiameter)))
)
abline(spruce.lm)


### Third graph
yhat=fitted(spruce.lm)

with(spruce.df,{
  segments(BHDiameter,Height,BHDiameter,yhat)
})



with(spruce.df,
     plot(Height~BHDiameter,bg="Blue",pch=21,ylim=c(0,1.1*max(Height)),xlim=c(0,1.1*max(BHDiameter)))
)

#make nieve model
with(spruce.df, abline(h=mean(Height)))
abline(spruce.lm)
#make the explained deviations (explained by the model)
with(spruce.df, segments(BHDiameter,mean(Height),BHDiameter,yhat,col="Red"))

### Fourth graph
with(spruce.df,
     plot(Height~BHDiameter,bg="Blue",pch=21,ylim=c(0,1.1*max(Height)),xlim=c(0,1.1*max(BHDiameter)))
)
with(spruce.df,abline(h=mean(Height)))
with(spruce.df, segments(BHDiameter,Height,BHDiameter,mean(Height),col="Green"))

TSS, MSS, and RSS

RSS=with(spruce.df,sum((Height-yhat)^2))
MSS=with(spruce.df,sum((yhat-mean(Height))^2))
TSS=with(spruce.df,sum((Height-mean(Height))^2))
print(paste0("TSS: ", TSS))
print(paste0("MSS: ", MSS))
print(paste0("RSS: ", RSS))

print(paste0("MSS/TSS: ", MSS/TSS))
print(paste0("TSS = MSS+RSS?: ", MSS + RSS, "  ... Yes."))

Task 5

Summary

summary(spruce.lm)

Slope

0.4815

Intercept

9.1468

Equation

HEIGHT = .4815*BHDiameter + 9.1468

Use predict()

predict(spruce.lm)

Prediction when diameter is 15cm: 18.7763

Prediction when diameter is 18cm: 11.9394

Prediction when diameter is 20cm: 19.1134

Task 6

library(ggplot2)
g=ggplot(spruce.df, aes(x=BHDiameter,y=Height,colour=BHDiameter))
g=g+geom_point() + geom_line()+ geom_smooth(method="lm")
g+ggtitle("Height Vs BHDiameter")

Task 7

"name of image"{ width=70% } "name of image"{ width=70% } "name of image"{ width=70% } "name of image"{ width=70% }



agracy2246/MATH4753grac0009 documentation built on April 26, 2020, 9:39 a.m.